<template>
	<view class="container">
		<view class="top">
			<view class="search flex-between">
				<input type="text" placeholder="请输入推广位名称" v-model="params.keyWord">
				<view class="btn tc" @click="search">
					搜索
				</view>
			</view>
			<view class="filter flex-start">
				<view class="li" @click="toFilter('taobaoId')">{{filter.taobaoId.label}}<text class="iconfont">&#xe618;</text></view>
				<view class="li" @click="toFilter('staffId')">{{filter.staffId.label}}<text class="iconfont">&#xe618;</text></view>
				<view class="li" @click="toFilter('label', 'position')">{{filter.label.label}}<text class="iconfont">&#xe618;</text></view>
			</view>
		</view>
		<view class="tip">
			温馨提示：只显示有订单数据的推广位
		</view>
		<view class="list">
			<view class="item" v-for="(item, index) in prodData.list" :key="index">
				<view class="ul flex-between">
					<view class="li tc">
						<view>推广位名称</view>
						<view>{{item.positionName}}</view>
					</view>
					<view class="li tc">
						<view>运营人</view>
						<view>{{item.username || '无'}}</view>
					</view>
					<view class="li tc">
						<view>标签</view>
						<view>{{item.label || '无'}}</view>
					</view>
				</view>
				<view class="bom flex-between">
					<text>联盟账号</text>
					<text>{{item.account || '无'}}</text>
				</view>
			</view>
			<!-- 加载文案 -->
			<LoadMore :loading="loading" :finished="finished" :empty="empty"></LoadMore>
		</view>

		<!-- 回到顶部 -->
		<view class="scrollTop" :class="[{ show: isShowScrollTop }]" @tap="backTop">
			<text class="iconfont">&#xe7ed;</text>
			<text class="text">置顶</text>
		</view>
	</view>
</template>

<script>
	import {
		loading,
		loaded
	} from '@/utils/tip'
	import LoadMore from '@/components/LoadMore.vue'
	export default {
		components: {
			LoadMore
		},
		data() {
			return {
				params: {
					page: 1,
					size: 10,
					taobaoId: '',
					staffId: '',
					label: '',
					order: '',
					sort: '',
					keyWord: ''
				},
				prodData: {
					list: [],
					pagination: null
				},
				loading: false,
				finished: false,
				empty: false,
				isShowScrollTop: false,
				// 过滤类型
				filterType: '',
				filter: {
					taobaoId: {
						value: '',
						label: '联盟账号'
					},
					staffId: {
						value: '',
						label: '运营人'
					},
					label: {
						value: '',
						label: '标签'
					}
				}
			};
		},
		onLoad() {

		},
		onShow() {
			this.params.keyWord = ''

			let data = uni.getStorageSync(this.filterType)

			if (!data) { // 按钮返回键什么都没选
				this.params[this.filterType] = this.params[this.filterType]
				this.filter[this.filterType] = this.filter[this.filterType]
			} else {
				this.params[this.filterType] = data.value
				this.filter[this.filterType] = data
			}
			
			this.clear()
			this.getData()
		},
		onPageScroll(e) {
			if (e.scrollTop > 350) {
				this.isShowScrollTop = true;
			} else {
				this.isShowScrollTop = false;
			}
		},
		// 上拉加载
		onReachBottom() {
			if (this.finished) return
			this.params.page++
			this.loading = true
			this.getData()
		},
		methods: {
			toFilter(val, labelType = '') {
				this.filterType = val
				uni.navigateTo({
					url: `/pages/common/check?type=${val}&values=${this.params[val] || 'all'}&labelType=${labelType}`
				})
			},
			search() {
				this.clear()
				this.getData()
				// this.params.keyWord = ''
			},
			getData() {
				loading()
				this.$service.pid.page(this.params).then((res) => {
					this.prodData.list = this.prodData.list.concat(res.list)
					this.prodData.pagination = res.pagination
					this.loadMore(res)
					loaded()
				}).catch((err) => {
					loaded()
				})
				uni.removeStorageSync(this.filterType)
			},
			//列表加载的一些逻辑判断
			loadMore(res) {
				if (res.list.length < this.params.size) this.finished = true
				if (this.prodData.list.length === 0) this.empty = true
				this.loading = false
			},
			clear() {
				this.params.page = 1
				this.prodData.list = []
				this.loading = false
				this.finished = false
				this.empty = false
			},
			// 回到顶部
			backTop() {
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 0
				})
			}
		}
	}
</script>

<style lang="scss">
	.container {
		.top {
			background-color: white;
			color: #999999;
			padding: 0 24rpx;
			padding-bottom: 0rpx;

			.search {
				input {
					width: 522rpx;
					height: 72rpx;
					border: 2rpx solid #F2F2F2;
					color: #999999;
					padding-left: 24rpx;
					box-sizing: border-box;
					border-radius: 8rpx;
				}

				.btn {
					width: 160rpx;
					height: 72rpx;
					background-color: #0C5DFF;
					color: white;
					line-height: 72rpx;
					border-radius: 8rpx;
					font-size: 28rpx;
				}
			}

			.filter {
				padding: 26rpx 0;
				text {
					color: #666666;
				}
				.li {
					width: 33%;
					text-align: center;
				}
			}
		}

		.tip {
			font-size: 20rpx;
			color: #999999;
			margin-top: 20rpx;
			padding: 0 24rpx;
		}

		.list {
			padding: 0 24rpx;

			.item {
				margin-top: 24rpx;
				padding: 38rpx 40rpx 16rpx 40rpx;
				font-weight: normal;

				.ul {
					margin-bottom: 36rpx;
				}

				.li {
					:nth-child(1) {
						font-size: 20rpx;
						margin-bottom: 10rpx;
						color: #999999;
					}
				}

				.bom {
					font-size: 28rpx;
					color: #999999;
					padding-top: 16rpx;
					border-top: 2rpx solid #F2F2F2;
				}
			}
		}
	}
</style>
